
const Koa = require('koa');
const router = require('koa-router')();
const path = require('path');
const koaTemplate = require('koa-art-template');
const koaStatic = require('koa-static');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');
const sd = require('silly-datetime');
const jsonp = require('koa-jsonp');

const app = new Koa();

// 引入路由模块
const index = require('./routers/index.js');
const admin = require('./routers/admin.js');
const api = require('./routers/api.js');


// 配置静态资源中间件
// app.use(koaStatic('.'));  // 如果配置这个表示项目目录就是静态资源目录（这个是不安全的）
app.use(koaStatic(__dirname + '/static'));


// 配置session中间件
app.keys = ['some secret hurr'];
const CONFIG = {
	key: 'koa:sess',
	maxAge: 1000 * 60 * 30,
	overwrite: true,
	httpOnly: true,
	signed: true,
	rolling: true,
	renew: false
}
app.use(session(CONFIG, app));



// 配置post数据中间件
app.use(bodyParser());



// 配置模板引擎
koaTemplate(app, {
	root: path.join(__dirname, 'views'),
	extname: '.html',
	debug: process.env.NODE_ENV !== 'production',
	dateFormat: dateFormat = function (value) {
		// 格式化日期过滤器
		return sd.format(new Date(value), 'YYYY-MM-DD HH:mm:ss');
	}
});


// 配置路由
router.use('/admin', admin);
router.use('/api', api)
router.use(index); 



app.use(router.routes());
app.use(router.allowedMethods());
app.listen(8088);
console.log('Server run http://localhost:8088/');